

import axios from 'axios'
// import { ElMessage } from 'element-plus'
import router from '../router/index'

// const baseUrl = 'https://scan.tokchain.org'
// 创建axios实例
const service = axios.create({
  // baseURL: baseUrl,
  timeout: 30000,
  crossDomain: true,
  withCredentials: true
})

service.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8';

service.interceptors.request.use(response => {
  return response;
},
  (error) => {
    let msg = "请求失败";
    switch (error.status) {
      case 0:
        msg = "请求地址错误";
        break;
      case 400:
        msg = "请求无效，请检查参数类型是否匹配";
        break;
      case 404:
        msg = "请求资源不存在，请检查路径是否正确";
        break;
      default:
        msg = "请求错误，请检查路径是否正确";
        break;
    }
    console.log(error)
    console.log('error')
    return Promise.reject(error);
  })

service.interceptors.response.use(
  response => {
    if (response.status == 200) {
      console.log('response-------')
      console.log(response)
      if (response.data.code && (response.data.code == 200 || response.data.code == 'Success' || response.data.code == 'InternalServerError')) {
        return response.data
      } else {
        router.push({ path: '/error', query: { a: 'not' } })
      }
    }

  },
  error => {
    router.push({ path: '/error', query: { a: 'not' } })
    return Promise.reject(error)
  }
)

export default service
